<template>
    <div class="one">
        <!-- 资讯中心顶部-公用 -->
        <Header></Header>
        <div class="indexContent">
            <div class="container">
                <!-- 面包屑导航 -->
                <div class="topNav" style="margin:20px 0;">
                    <el-breadcrumb separator-class="el-icon-arrow-right">
                        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                        <el-breadcrumb-item>全文检索</el-breadcrumb-item>
                    </el-breadcrumb>
                </div>
                <!-- 主要内容 -->
                <div class="newContent">
                    <div class="newSearch">
                        <h2 class="newH2">全文检索</h2>
                        <div class="searchOne">
                            <el-input placeholder="请输入关键字" v-model="params.keyword" class="input-with-select" clearable
                                @clear="queryKet('', '')" @keyup.enter.native="seachEnter">
                                <el-select v-model="params.modelcode" slot="prepend" placeholder="请选择" @change="changeVal">
                                    <el-option label="政策资讯" value="0"></el-option>
                                    <el-option label="金融产品" value="1"></el-option>
                                    <el-option label="服务" value="2"></el-option>
                                </el-select>
                                <el-button slot="append" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                            </el-input>
                        </div>
                    </div>
                    <!-- 服务 -->
                    <div class="itemList" v-if="params.modelcode === '2'">
                        <div class="itemNone" v-if="knowList.length == 0">
                            <p>目前暂无数据</p>
                        </div>
                        <ul v-else>
                            <template>
                                <li v-for="item in knowList" :key="item.id" @click="toDetailLink(item.path)">
                                    <p class="itemTitle" v-html="item.title">
                                    </p>
                                    <p class="itemDate">{{ item.postDate }}</p>
                                </li>
                            </template>
                            <!-- 交易 -->
                        </ul>
                        <div class="newPage">
                            <el-pagination background @size-change="handleSizeChange2"
                                @current-change="handleCurrentChange2" :current-page="serviceParams.page"
                                :page-size="serviceParams.pageSize" layout="total, prev, pager, next, jumper"
                                :total="total2">
                            </el-pagination>
                        </div>
                    </div>
                    <div class="itemList" v-else>
                        <div class="itemNone" v-if="knowList.length == 0">
                            <p>目前暂无数据</p>
                        </div>
                        <ul v-else>
                            <template>
                                <li v-for="item in knowList" :key="item.id" @click="toDetail2(item.id, params.modelcode)">
                                    <p class="itemTitle" v-html="item.title">
                                    </p>
                                    <p class="itemDate">{{ item.postDate }}</p>
                                </li>
                            </template>
                            <!-- 交易 -->
                        </ul>
                        <div class="newPage">
                            <el-pagination background @size-change="handleSizeChange"
                                @current-change="handleCurrentChange" :current-page="params.page"
                                :page-size="params.pageSize" layout="total, prev, pager, next, jumper"
                                :total="total">
                            </el-pagination>
                        </div>
                    </div>
                    <!-- 分页 -->
                </div>
            </div>
        </div>
        <!-- 底部 -->
        <Footer></Footer>
    </div>
</template>
<script>
import Footer from "@/components/home/footer/footer";
import Header from "@/components/home/header/header";
import { getSearch, searchService } from "@/request/api/link"
export default {
    components: {
        Footer,
        Header,
    },
    data() {
        return {
            params: {
                keyword: '',
                page: 1,
                modelcode: '0',//0政策，1金融产品，2服务
                pageSize: 10
            },
            serviceParams: {
                page: 1,
                pageSize: 10,
                title: ''//名称
            },
            selectVal: "0",
            knowList: [],
            total: 0,
            total2: 0,
            val: this.$route.params.val,
            type: this.$route.params.type
        };
    },
    created() {
        if (this.$route.params.val) {
            this.params.keyword = this.val
            this.params.modelcode = this.type
            this.params.page = 1;
						
            if (this.params.modelcode === '2') {
                this.serviceParams.title = this.val
                this.serviceParams.page = 1
                this.getService()
            } else {
                this.getKnowList()
            }
        }
    },
    methods: {
        // 获取列表-资讯和金融产品
        getKnowList() {
            getSearch(this.params).then((res) => {
                if (res.data.code == 200) {
                    this.knowList = res.data.result.list;
                    this.total = res.data.result.total;
                }
            });
        },
        // 获取列表-服务
        getService() {
            searchService(this.serviceParams).then((res) => {
                if (res.data.code == 200) {
                    this.knowList = res.data.result.items;
                    this.total2 = res.data.result.total;
                }
            });
        },
        // 回车键搜索
        seachEnter(e) {
            var keyCode = window.event ? e.keyCode : e.which;
            if (keyCode == 13) {
                if (this.params.modelcode === '2') {//服务
                    this.serviceParams.title = this.params.keyword
                    this.serviceParams.page = 1
                    this.getService();
                } else {
                    this.params.page = 1;
                    this.getKnowList();
                }
            }
        },
        // 资讯-产品分页
        handleSizeChange(val) {
            this.params.pageSize = val;
            this.params.page = 1;
            this.getKnowList();
        },
        handleCurrentChange(val) {
            this.params.page = val;
            this.getKnowList();
        },
        changeVal(val) {
            this.params.modelcode = val
            this.params.page = 1;
            this.knowList = []
            this.total = 0
            this.total2 = 0
        },
        // 搜索
        handleSearch() {
            if (this.params.keyword === "") {
                this.$message({
                    message: "请输入关键字！",
                    type: "warning",
                });
                return false;
            }
            if (this.params.modelcode === '2') {//服务
                this.serviceParams.title = this.params.keyword
                this.serviceParams.page = 1
                this.getService();
            } else {
                this.params.page = 1;
                this.getKnowList();
            }

        },
        // 监听iput清除事件
        queryKet() {
            this.params.keyword = null
            this.knowList = []
            this.total = 0
        },
        // 查看详情
        toDetail2(id, type) {
            if (type === '0') {//资讯
                this.$router.push({
                    name: "newDetail",
                    query: {
                        id,
                    },
                });
            } else if (type === '1') {//金融产品
                this.$router.push({
                    name: "loanDetail",
                    query: {
                        id,
                    },
                });
            }

        },
        // 服务分页
        handleSizeChange2(val) {
            this.serviceParams.pageSize = val;
            this.serviceParams.page = 1;
            this.getService();
        },
        handleCurrentChange2(val) {
            this.serviceParams.page = val;
            this.getService();
        },
        toDetailLink(path) {
            this.$router.push(path);
        }
    },
};
</script>
<style lang="less" scoped>
@import "./fullSearch.less";
</style>
  